package com.common.frame.widget

import android.content.Context
import android.view.Gravity
import androidx.core.view.isVisible
import com.common.frame.R
import com.common.frame.base.BaseDialog
import com.common.frame.base.setHtmlText
import com.common.frame.bean.TipsEvent
import com.common.frame.databinding.DialogTipsBinding
import com.common.frame.utils.ScreenUtil


class TipsDialog(context: Context) : BaseDialog<DialogTipsBinding>(context) {

    lateinit var listener: (TipsEvent) -> Unit

    var event: TipsEvent? = null

    override fun initView() {
        setWidthHeight(ScreenUtil.getScreenWidth(context))
    }

    override fun initListener() {
        mBinding.close.setOnClickListener { dismiss() }
        mBinding.cancel.setOnClickListener { dismiss() }
        mBinding.open.setOnClickListener {
            if (this::listener.isInitialized) {
                event?.let { listener(it) }
            }
            dismiss()
        }
    }

    override fun getLayoutId(): Int = R.layout.dialog_tips

    override fun getGravity(): Int = Gravity.CENTER

    fun show(event: TipsEvent,showCancel:Boolean = false) {
        show()
        this.event = event
        mBinding.tvContent.setHtmlText(event.tips)
        mBinding.open.text = event.button
        mBinding.cancel.isVisible = showCancel
    }
}